package de.haw.dbp.a4.playground;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import dc.connectionmanager.database.DBConnectionManager;
import dc.connectionmanager.logic.ACommand;
import dc.connectionmanager.logic.CArgument;
import dc.connectionmanager.logic.ICallback;
import dc.connectionmanager.logic.IConnectionManager;
import dc.connectionmanager.logic.IUpdaterTriggerFunc;

public class JPanelSimpleSQLExecuter extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2773013901546828014L;
	
	private JFrame parent;
	private DBConnectionManager dbcm;
	
	private JTextField tf_inputQuery;
	private JTextArea ta_answer;
	private JButton b_send;
	
	private DBCommand dbcommand;
	
	public JPanelSimpleSQLExecuter(JFrame pa, DBConnectionManager pDBcm)
	{
		this.parent = pa;
		this.dbcm = pDBcm;
		
		dbcm.registerConnTriggerFunc(new IUpdaterTriggerFunc() {
			
			@Override
			public void handlerUpdateTrigger() 
			{
				String str = "";
				str += "Open Connections: ";
				str += dbcm.getCountConnections() + " ";
				str += "(Min: " + dbcm.getMinConnection();
				str += " / Max: " + dbcm.getMaxConnection() + ")";
				
				System.out.println(str);				
			}
		});
		
		dbcommand = new DBCommand(dbcm);
		
		this.initGUIElements();
	}
	
	private void initGUIElements()
	{
		//Panel einrichten
		this.setLayout(null);
		this.setLocation(0,0);
		this.setSize(parent.getSize());
				
		//Eingabe Textfeld einrichten
		tf_inputQuery = new JTextField();
		tf_inputQuery.setLocation(10, this.getSize().height - 105);
		tf_inputQuery.setSize(670, 25);
		
		//Senden Button 
		b_send = new JButton("Senden");
		b_send.setLocation((tf_inputQuery.getLocation().x + tf_inputQuery.getSize().width + 10), (tf_inputQuery.getLocation().y));
		b_send.setSize(90, 25);
		
		b_send.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				if(tf_inputQuery.getText().length() > 0)
				{
					dbcommand.exec_query( new ICallback() 
					{
						
						@Override
						public void handleResult(Object arg0) 
						{
							
							
							ArrayList<ArrayList<String>> result = (ArrayList<ArrayList<String>>) arg0;
							
							if(result != null)
							{
								ta_answer.setText("");
								for(int i = 0; i < result.size(); ++i)
								{
									for(int j = 0; j < result.get(i).size(); ++j)
									{
										ta_answer.append(result.get(i).get(j) + " ");
									}
									ta_answer.append("\n");
								}
							}
							else
							{
								ta_answer.setText("Die Anfrage brachte kein Ergebnis");
							}
						}
					}, tf_inputQuery.getText());
				}
				
			}
		});
		
		
		//TextArea
		ta_answer = new JTextArea();
		ta_answer.setLocation(10, 10);
		ta_answer.setSize((this.getSize().width -30), (380));
		ta_answer.setEditable(false);
		
		//Komponenten zusammenführen
		this.add(tf_inputQuery);
		this.add(ta_answer);
		this.add(b_send);
	}
	
	@Override
	public void setEnabled(boolean arg0)
	{
		super.setEnabled(arg0);
		for(Component com : this.getComponents())
		{
			com.setEnabled(arg0);
		}
		
	}
	
	public class DBCommand extends ACommand 
	{

		public DBCommand(IConnectionManager cmHandle) 
		{
			super(cmHandle);
		}

		public void exec_query(ICallback cb, String query)
		{
			this.sendCommand(new CArgument(cb, query));
		}
		

	}
}
